dojo.provide("dojot.util.PageInfo");
/**
<pageInfo>
  <currentPage>1</currentPage> 
  <itemCount>2</itemCount> 
  <itemCountPerPage>1</itemCountPerPage> 
  <pageCount>2</itemCountPerPage> 
  <listPageCount>10</listPageCount> 
  <listPageStart>1</listPageStart> 
  <listPageEnd>2</listPageEnd> 
  <startPoint>0</startPoint> 
  <fetchCount>1</fetchCount> 
  </pageInfo>
**/


dojot.util.PageInfo = dojo.extend(function(req){
        this.items = [];
        dojo.mixin(this,req);
        this._init();
    },{
        //动态分页的话，需要在PageInfo里面维护数据。
	items:[],
         // 当前页号(从1开始)
	currentPage: 1,
        // 总记录数
	itemCount:0,
        // 每页记录数
	itemCountPerPage: 5,
         // 总页数
	pageCount: 0,			
	startPoint: 0,
         // 页码导航条中列出的页数 
	listPageCount:5,		
	listPageStart:0,
	listPageEnd:0,
	_init:function(){
            //根据传递进来的参数，开始进行分页处理
            this.itemCount = this.items.length;
            if(this.itemCount == 0)return;
            //初始化有多少页
            var pageCount = this.itemCount / this.itemCountPerPage;
            this.pageCount = (this.itemCount % this.itemCountPerPage == 0) ? pageCount : Math.ceil(pageCount);
	},
	setCurrentPage: function(currentPage){
            this.currentPage = currentPage || this.currentPage;
            this._normalizeCurrentPage();
	},
	getCurrentPage: function() {
            this._normalizeCurrentPage();
            return this.currentPage;
	},
        _normalizeCurrentPage:function(){
            //规整currentPage
            // 当前页不能比总分页数大
            if (this.currentPage > this.pageCount) {
                this.currentPage = this.pageCount;
            }
            // 当前页不能比1小
            if (this.currentPage < 1) {
                this.currentPage = 1;
            }
        },
	getStartPoint: function() {
            var result = (this.getCurrentPage()-1) * this.itemCountPerPage;
            return result < 0 ? 0 : result;
	},
	getEndPoint: function(){
            var result = this.getStartPoint() + this.itemCountPerPage;
            return this.itemCount >= result ? result : this.itemCount;
	},
	getListPageStart: function() {
            var listPageStart = 0;
            if (this.getCurrentPage() < this.listPageCount) {
                    listPageStart = 1;
            } else if (this.getCurrentPage() > (this.pageCount - this.listPageCount / 2)){
                    listPageStart = (this.pageCount - this.listPageCount + 1);
            } else {
                    listPageStart = (this.getCurrentPage() - this.listPageCount / 2 + 1);
            }
            return listPageStart;
	},
	getListPageEnd: function(){
            var listPageEnd = 0;
            if (this.getCurrentPage() < this.listPageCount) {
                    listPageEnd = this.listPageCount;
            } else {
                    listPageEnd = (this.getCurrentPage() + this.listPageCount / 2);
            }
            return listPageEnd < this.pageCount ? listPageEnd : this.pageCount;
	},
	getCurrentItems:function(){
            console.log(this.items);
            console.log(this.getStartPoint(),this.getEndPoint());
            console.log(this.items.slice(this.getStartPoint(),this.getEndPoint()));
		return this.items.slice(this.getStartPoint(),this.getEndPoint());
        }
    }
);

